﻿using System;
using System.Collections.Generic;
using System.Linq;
using Sustainalytics.Reporting.Framework;
using Foxit.PDF.PageElements;
using Foxit.PDF;

namespace Sustainalytics.ReportGenerator.BL
{
    public class PdfReportSectionGenerator : IPdfReportSectionGenerator
    {
        public PdfReport _report;
        public PdfReportSection _reportSection;
        public List<Reporting.Framework.OutlinePosition> _outlinePositions;
        public List<Reporting.Framework.PageElementPosition> _pageElementPositions;
        public TearSheetReportDataSource _dataSource;

        public const float EmptyRowHeight = 10f;

        #region Static Images

        private static byte[] _companyLogoHeaderImage;
        public byte[] CompanyLogoHeaderImage => _companyLogoHeaderImage;

        private static void LoadStaticImages()
        {
            if (_companyLogoHeaderImage == null)
                _companyLogoHeaderImage = ImageManager.GetJpgImage(@"Images\sustaynalytics_logo_header.jpg");
        }

        #endregion

        static PdfReportSectionGenerator()
        {
            LoadStaticImages();
        }
        


        public PdfReportSection GeneratePdfReportSection(object objectDataSource)
        {
            _dataSource = objectDataSource as TearSheetReportDataSource;

            if (_dataSource == null || _dataSource.CompanyId == null)
            {
                throw new ArgumentException("ReportDataSource does not contain the necessary data for report generation!");
            }

            InitializeReportSection();

            ComposeSectionContent();

            if (_report.Pages.Any())
                _reportSection.PdfDocument = _report.Draw();

            return _reportSection;
        }

        public void InitializeReportSection()
        {
            _report = new PdfReport();

            _outlinePositions = new List<Reporting.Framework.OutlinePosition>();
            _pageElementPositions = new List<Reporting.Framework.PageElementPosition>();

            _reportSection = new PdfReportSection
            {
                Id = Guid.NewGuid(),
                Name = "Sources Section",
                PdfDocument = new byte[0],
                LastUpdate = DateTime.UtcNow,
                OutlinePositions = _outlinePositions,
                PageElementPositions = _pageElementPositions
            };
        }

        public virtual void ComposeSectionContent()
        {
        }


        public void AddHeaderToPage(Page page)
        {
            const float top = -20f;
            var width = _report.PageMaxWidth;
            const float tableHeight = 125f;
            const float rowHeight = 10f;

            var companyName = $"<p><b>{_dataSource.CorporateData.CompanyName}</b></p>";


            var table = GetDefaultTable(_report.LeftMargin, top, width, tableHeight + 10f);

            table.Columns.Add(width);


            var row = table.Rows.Add(15f);
            row.Cells.Add(new Rectangle(0f, 0f, width, 15f, new RgbColor(252, 175, 23), new RgbColor(252, 175, 23)));

            row = table.Rows.Add(20f);
            row.Cells.Add(string.Empty);

            row = table.Rows.Add(20f);
            row.Cells.Add(new Label("Environmental, Social, and Governance (ESG) Report", 0f, 0f, width,
                11f + 2f, Fonts.CalibriBold, 11f, TextAlign.Left, RgbColor.Black));

            row = table.Rows.Add(60f);
            row.Cells.Add(new FormattedTextArea(companyName, 0f, 0f, 320f, 60f, Fonts.CalibriFontFamily, 16f, false));

            row = table.Rows.Add(rowHeight);
            var cell = row.Cells.Add(string.Empty);
            cell.Align = TextAlign.Left;
            cell.VAlign = VAlign.Center;
            cell.Border.Bottom.LineStyle = LineStyle.Solid;
            cell.Border.Bottom.Width = 1f;
            cell.Border.Bottom.Color = RgbColor.Black;

            page.Elements.Add(table);
        }


        public static Table GetDefaultTable(float left, float top, float width, float height)
        {
            var table = new Table(left, top, width, height);
            table.Border.Width = 0.0f;
            table.Border.Color = Grayscale.Black;
            table.Border.LineStyle = LineStyle.None;
            table.BackgroundColor = RgbColor.White;
            table.CellSpacing = 0.0f;
            table.CellDefault.Align = TextAlign.Left;
            table.CellDefault.VAlign = VAlign.Top;
            table.CellDefault.Padding.Value = 0.0f;
            table.CellDefault.Border.Width = 0.0f;
            table.CellDefault.Border.LineStyle = LineStyle.None;
            table.CellDefault.Border.Color = RgbColor.White;

            return table;
        }


    }
}
